package larrys.pomodoro.util;

import larrys.pomodoro.domain.entity.PomodoroUser;
import larrys.pomodoro.security.entity.PomodoroUserDetailsAdapter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {

	private static Log logger = LogFactory.getLog(SecurityUtils.class);

	public static PomodoroUser getCurrentUser() {
		logger.debug("Trying to get current user");
		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();

		if (principal instanceof PomodoroUserDetailsAdapter) {
			logger.debug("Returning:" + principal);
			return ((PomodoroUserDetailsAdapter) principal).getUser();
		} else {
			String msg = "Principal is not instanceof PomodoroUserDetailsAdapter!";
			logger.error(msg);
			throw new RuntimeException(msg);
		}

	}

}
